<?php
/*************************************************************************
 * Zero PHP
 * Copyright (c) 2012
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * Author: zlk
 *************************************************************************/
 class Idispatcher extends CZero implements dispatcher
 {

 	public function __construct(){
 	}
 	
 	public static function init(){
 		//二级域名常量
 		define('SUB_DOMAIN', self::parseDomain());
 		//url解析
 		$var=self::parseUrl();
 	}

//解析url
 	public static function parseUrl(){
 		$model=C('zero.Url_Model');
 		$var=array();
 		//检查是否启用二级域名分组
 		if(C('zero.Sub_Domain_Group_Check')==1){
 				$var[C('zero.S_GROUP')]=in_array(SUB_DOMAIN,explode(",",C('zero.Sub_Domain_Group')))?SUB_DOMAIN:'';
 		}
 		//普通模式
 		if($model==1){
 			$get=$_GET?$_GET:$_POST;
 			$var[C('zero.S_GROUP')]=isset($get['g'])?isset($get['g']):'';
 			$var[C('zero.S_ACTION')]=isset($get[C('zero.S_ACTION')])?$get[C('zero.S_ACTION')]:C('zero.D_ACTION');
 			$var[C('zero.S_MODULE')]=isset($get[C('zero.S_MODULE')])?$get[C('zero.S_MODULE')]:C('zero.D_MODULE');
 			$var=array_merge($get,$var);
 		}
 		//重写pathinfo模式
 		elseif($model==2){
 			$depr=C('zero.Url_Depr')?C('zero.Url_Depr'):'/';
 			if(!isset($_SERVER['PATH_INFO'])) $_SERVER['PATH_INFO']='/'.C('zero.D_MODULE').'/'.C('zero.D_ACTION');
 			$url_info = explode($depr, trim($_SERVER['PATH_INFO'], '/'));
 			//判断是否为组
 			if(C('zero.Group_List')&&!isset($var[C('zero.S_GROUP')])){echo 1;
 				$var[C('zero.S_GROUP')]=in_array(strtolower($url_info[0]),explode(",",strtolower(C('zero.Group_List'))))?array_shift($url_info):'';
 			}
 			$var[C('zero.S_MODULE')]=array_shift($url_info);
 			$var[C('zero.S_ACTION')]=array_shift($url_info);
 			//解析剩余url,写入$var
 			$res = preg_replace('@(\w+)'.$depr.'([^'.$depr.'\/]+)@e', '$var[\'\\1\']="\\2";', implode($depr,$url_info));
 		}else{
 			
 		}
 		//判断不能访问的模块
 		 if(C('zero.Group_Deny')&& isset($var[C('zero.S_GROUP')])&& in_array(strtolower($var[C('zero.S_GROUP')]),explode(',',strtolower(C('zero.Group_Deny'))))) {
                    exit;
            }
 		defined('GROUP_NAME') or define('GROUP_NAME',isset($var[C('zero.S_GROUP')])?$var[C('zero.S_GROUP')]:'');
		defined('MODULE_NAME') or define('MODULE_NAME',isset($var[C('zero.S_MODULE')])?$var[C('zero.S_MODULE')]:'');
		defined('ACTION_NAME') or define('ACTION_NAME',isset($var[C('zero.S_ACTION')])?$var[C('zero.S_ACTION')]:'');
 		return ;
 	}
 	/**
 	 * 解析域名
 	 */
 	public static function parseDomain(){
 		if(C('zero.Sub_Domain')){
 			$subDomain = strtolower(substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.')));
 			return $subDomain;
 		}
 	}
 	/**
 	 * 获取分组
 	 */ 	
 	public static function getGroup($var){
			
 	}
 	/**
 	 * 获取模块
 	 */
	public static function getModule($var) {
		
	}

	/**
	 * 获取操作
	 */
	public static function getAction($var) {
		
	}
 	
 }
?>
